package org.kca.yarmour.data;

import java.util.ArrayList;
import java.util.List;

import org.kca.yarmour.data.GlobalData.CargoType;
import org.kca.yarmour.data.GlobalData.FactionType;

public class Town
{
    private String id, name;
    private WorldLocation location;
    private int factionLevel, tradeLevel, mechLevel;
    private List<String> troopsForHire, vehsForSale;
    private int[] buyPrices, sellPrices, supply;
    
    public Town(String id, String name, WorldLocation location)
    {
        this.id = id;
        this.name = name;
        this.location = location;
        troopsForHire = new ArrayList<String>();
        vehsForSale = new ArrayList<String>();
        buyPrices = new int[CargoType.getNumTypes()];
        sellPrices = new int[CargoType.getNumTypes()];
        supply = new int[CargoType.getNumTypes()];
    }
    
    /**
     * Initializes the supply in this town. Right now it initializes to a fixed
     * value of 200.
     */
    public void initSupply()
    {
        for (int i = 0; i < CargoType.getNumTypes(); i++)
        {
            supply[i] = 200;
        }
    }
    
    /**
     * Sets the price for a particular cargo type
     * @param cargoType the cargo type
     * @param price the buying price for this cargo
     * @param buySellRatio the selling price expressed as a percentage of the buying price
     */
    public void setPrice(int cargoType, int price, float buySellRatio)
    {
        int sprice = Math.round(price * buySellRatio);
        // Selling price must be at least 1 below buy price
        sprice = Math.min(sprice, price - 1);
        buyPrices[cargoType] = price;
        sellPrices[cargoType] = sprice;
    }

    /**
     * Gets the level of the faction in this town
     * @return the factionLevel
     */
    public int getFactionLevel()
    {
        return factionLevel;
    }
    
    /**
     * Sets the level of the faction in this town
     * @param factionLevel the factionLevel to set
     */
    public void setFactionLevel(int factionLevel)
    {
        this.factionLevel = factionLevel;
    }
    
    /**
     * Gets the level of the merchant in this town
     * @return the tradeLevel
     */
    public int getTradeLevel()
    {
        return tradeLevel;
    }
    
    /**
     * Sets the level of the merchant in this town
     * @param tradeLevel the tradeLevel to set
     */
    public void setTradeLevel(int tradeLevel)
    {
        this.tradeLevel = tradeLevel;
    }
    
    /**
     * Gets the level of the mechanic in this town
     * @return the mechLevel
     */
    public int getMechLevel()
    {
        return mechLevel;
    }
    
    /**
     * Sets the level of the mechanic in this town
     * @param mechLevel the mechLevel to set
     */
    public void setMechLevel(int mechLevel)
    {
        this.mechLevel = mechLevel;
    }
    
    /**
     * Gets the list of troops for hire in this town
     * @return the list of troops for hire
     */
    public List<String> getTroopsForHire()
    {
        return troopsForHire;
    }
    
    /**
     * Gets the list of vehicles for sale in this town
     * @return the list of vehicles for sale
     */
    public List<String> getVehiclesForSale()
    {
        return vehsForSale;
    }
    
    /**
     * Gets the buy price for a type of cargo
     * @param cargoType the cargo type
     * @return the buy price for that cargo type
     */
    public int getBuyPrice(int cargoType)
    {
        if (cargoType < 0 || cargoType > CargoType.getNumTypes())
        {
            return -1;
        }
        return buyPrices[cargoType];
    }
    
    /**
     * Gets the sell price for a type of cargo
     * @param cargoType the cargo type
     * @return the sell price for that cargo type
     */
    public int getSellPrice(int cargoType)
    {
        if (cargoType < 0 || cargoType > CargoType.getNumTypes())
        {
            return -1;
        }
        return sellPrices[cargoType];
    }
    
    /**
     * Gets the supply of a type of cargo
     * @param cargoType the cargo type
     * @return the supply of that cargo type
     */
    public int getSupply(int cargoType)
    {
        if (cargoType < 0 || cargoType > CargoType.getNumTypes())
        {
            return -1;
        }
        return supply[cargoType];
    }
    
    /**
     * Sets the supply of a cargo type
     * @param cargoType the cargo type
     * @param amount the amount of that cargo type to set
     */
    public void setSupply(int cargoType, int amount)
    {
        if (amount < 0 || cargoType < 0 || cargoType > CargoType.getNumTypes())
        {
            return;
        }
        supply[cargoType] = amount;
    }
    
    /**
     * Gets the id of this town
     * @return the id
     */
    public String getId()
    {
        return id;
    }
    
    /**
     * Gets the name of this town
     * @return the name
     */
    public String getName()
    {
        return name;
    }
    
    /**
     * Gets the location of this town
     * @return the location
     */
    public WorldLocation getLocation()
    {
        return location;
    }
}
